// src/utils下创建i18n/index.js
import Vue from "vue";
import VueI18n from "vue-i18n";

// 引入各个语言配置文件
import th from "./th";
import en from "./en";
import pt from "./pt";

Vue.use(VueI18n);

// 获取当前域名
const currentHostname = window.location.hostname;
const currentQuery = window.location.search;
let isShowEn = true;

// 判断域名逻辑
if (currentHostname === "www.luckvictorious.com") {
  isShowEn = false;
} else {
  isShowEn = true;
}
const params = new URLSearchParams(currentQuery);
let resultParams = "";
// 遍历参数并输出
params.forEach((value) => {
  resultParams = decodeURIComponent(value);
});

let currentLanguage = resultParams
  ? resultParams
  : localStorage.getItem("accept_language");

// decodeURIComponent
if (!currentLanguage) {
  if (!isShowEn) {
    localStorage.setItem("accept_language", "th");
  } else {
    localStorage.setItem("accept_language", "en");
  }
} else {
  localStorage.setItem("accept_language", currentLanguage);
}

// 创建vue-i18n实例i18n
const i18n = new VueI18n({
  // 设置默认语言
  locale: localStorage.getItem("accept_language") || "en", // 语言标识
  // 添加多语言（每一个语言标示对应一个语言文件）
  messages: {
    en,
    th,
    pt,
  },
});
// 暴露i18n
export default i18n;
